/**  
 * @Title: CacheUtils.java
 * @Package: com.itic.appbase.framework.utils
 * @author: idong
 * @date: 2015年6月4日 下午1:22:43
 * @version: V1.0  
 */

package com.itic.appbase.framework.utils;

import org.slf4j.Logger;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.plugins.logger.Log;

/**
 * <p>
 * Company: itic
 * </p>
 * 基于Spring+EhCache的缓存操作工具类.
 * 
 * @author: idong
 * @date: 2015年6月4日 下午1:22:43
 * @version: V1.0
 */
public class CacheHelper {

    private static final CacheManager cacheManager = (CacheManager) SpringContextHelper.getBean("cacheManager");

    /**
     * 日志对象
     */
    private static final Logger LOGGER = Log.get(CacheHelper.class);

    private CacheHelper() {
        super();
    }

    /**
     * 获取SYS_CACHE缓存
     * 
     * @param key
     * @return
     */
    public static Object get(String key) {
        return get(Constants.SYS_CACHE, key);
    }

    /**
     * 写入SYS_CACHE缓存
     * 
     * @param key
     * @return
     */
    public static void put(String key, Object value) {
        put(Constants.SYS_CACHE, key, value);
    }

    /**
     * 从SYS_CACHE缓存中移除
     * 
     * @param key
     * @return
     */
    public static void remove(String key) {
        remove(Constants.SYS_CACHE, key);
    }

    /**
     * 获取缓存
     * 
     * @param cacheName
     * @param key
     * @return
     */
    public static Object get(String cacheName, String key) {
        if (null != cacheManager) {
            Element element = getCache(cacheName).get(key);
            if (null != element) {
                Log.debug(LOGGER, "Cache Get: {},{}", cacheName, key);
                return element.getObjectValue();
            }
        }
        return null;
    }

    /**
     * 写入缓存
     * 
     * @param cacheName
     * @param key
     * @param value
     */
    public static void put(String cacheName, String key, Object value) {
        if (null != cacheManager) {
            Element element = new Element(key, value);
            Log.debug(LOGGER, "Cache Put: {},{}", cacheName, key);
            getCache(cacheName).put(element);
        }
    }

    /**
     * 从缓存中移除
     * 
     * @param cacheName
     * @param key
     */
    public static void remove(String cacheName, String key) {
        if (null != cacheManager) {
            Log.debug(LOGGER, "Cache Remove: {},{}", cacheName, key);
            getCache(cacheName).remove(key);
        }
    }

    /**
     * 获得一个Cache，没有则创建一个。
     * 
     * @param cacheName
     * @return
     */
    private static Cache getCache(String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        if (null == cache) {
            cacheManager.addCache(cacheName);
            cache = cacheManager.getCache(cacheName);
            cache.getCacheConfiguration().setEternal(true);
        }
        return cache;
    }

    /**
     * 获取Spring cache manager
     * 
     * @return
     */
    public static CacheManager getCacheManager() {
        return cacheManager;
    }
}
